package ma.controller.panels.parameterpanels;


import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.model.ManufacturingMethodInterface;
import ma.view.gui.EBenchmarkPanel;
import ma.view.gui.parameterPanels.InputPanel;

/**
 * A {@code CtrInPanel} controls the behavior of an input
 * parameter panel. It shows status messages and controls the button which
 * starts the calculation.
 * @author Oliver Eckstein
 *
 */
public abstract class CtrInPanel 
    extends CtrParamPanel {
        
    private static int invalidInputs = 0;
    private static int validInputs = 0;
    protected InputPanel panel;
    
    public CtrInPanel(
            ManufacturingMethodInterface manuMethod,
            CtrInformationPanel infoPanelCtr) {
        super(manuMethod, infoPanelCtr);
        addTextFieldListenersAndRegisterObservers();
    }

    /**
     * To add a document listener to every text field of a parameter panel.
     * The view of this controller gets registered as an observer in the model.
     * This method is just for the parameters that have been created in the
     * model when creating a new instance of it. It should only be called once.
     */
    protected abstract void addTextFieldListenersAndRegisterObservers();
    
    /**
     * To tell the controller that another invalid input was written into
     * one of the text fields.
     */
    public void addInvalidInput() {
        invalidInputs++;
        infoPanelCtr.getView().showInvalidInputError(invalidInputs);       
        panel.setErrorStatus(true);
        infoPanelCtr.disableStoreData();
    }
    
    /**
     * To tell the controller that another invalid input was corrected.
     */
    public void removeInvalidInput() {
        invalidInputs--;        
        if(invalidInputs == 0) {
            infoPanelCtr.getView().shotManipulateMethodStandardMessage();
            panel.setErrorStatus(false);
            infoPanelCtr.enableStoreData();            
        } else {
            infoPanelCtr.getView().showInvalidInputError(invalidInputs);
        }        
    }
    
    /**
     * To tell the controller that another valid input was written.
     */
    public void addValidInput() {        
        validInputs++;
        if(manuMethod.allSet()) {
            infoPanelCtr.enableCalculation();            
        }        
        
        if (invalidInputs == 0) {
            infoPanelCtr.enableStoreData();
        }
    }
    
    /**
     * To tell the controller that another valid input was removed.
     */
    public void removeValidInput() {   
        validInputs--;
        infoPanelCtr.disableCalculation();        
    }
    
    /**
     * To get the number of invalid inputs in the view.
     * @return number of invalid inputs.
     */
    public int getInvalidInputs() {
        return invalidInputs;
    }
    
    /**
     * To get the number of valid inputs done by the user.
     * @return number of valid inputs
     */
    public int getValidInputs() {
        return validInputs;
    }
    
    @Override
    /*
     * (non-Javadoc)
     * @see ma.controller.gui.Controllable#getView()
     */
    public EBenchmarkPanel getView() {
        return panel;
    }
}
